/**************************************************************************/
/*                                                                        */
/* Copyright (c) SinoEmbed Technology Co.,Ltd.           */
/*  */
/*                                                                        */
/*                                                                        */
/*    created:    2012-6-27                                             */ 
/*    filename:   SinoembedPuzzleWnd.cpp                                          */ 
/*    author:     Derek Huang                                                  */  
/*    purpose:                                                            */ 
/*                                                                        */
/* ************************************************************************/
#include "MPEGShellWnd.h"
#include "ShellWnd.h"

#ifndef MERGER_APP_TO_SHELL
#else
//extern CMPEGShellWnd		* g_pMPEGShellWnd;
CMPEGShellWnd		* pgMPEGShellWnd;
extern HINSTANCE			g_SDHResModuleHandle;
extern DWORD				g_BkgndID;
#endif

#define WM_SOURCE_EVENT  (WM_USER + 250)
#define WM_SOURCE_SHOWWINDOW (WM_USER + 251)


CMPEGShellWnd::CMPEGShellWnd()
{
	pgMPEGShellWnd = (CMPEGShellWnd*)this;
}
CMPEGShellWnd::~CMPEGShellWnd()
{

}

#ifdef MERGER_APP_TO_SHELL
///////////////////////////////////////////////////////////////////////////
/// For the thread call

BOOL CMPEGShellWnd::Create()
{
	if(!CMzWndEx::Create(0,0,800,480, 0, 0, 0))
		return FALSE;

	//jdhuang 20091104
	Show(true, true);
	return TRUE;
}

#endif
void CMPEGShellWnd::PaintWin(HDC hdc, RECT* prcUpdate )
{
	BitBlt(hdc, prcUpdate->left, prcUpdate->top,
		RECT_WIDTH(*prcUpdate), RECT_HEIGHT(*prcUpdate),
		m_hBakDC, prcUpdate->left, prcUpdate->top, SRCCOPY);
	CMzWndEx::PaintWin( hdc, prcUpdate );
}
BOOL CMPEGShellWnd::OnInitDialog()
{
	if( !CMzWndEx::OnInitDialog() )
	{
		return FALSE;
	}
	::SetWindowText(m_hWnd, L"CMPEGShellWnd");
//	SetForegroundWindow( m_hWnd );

	LoadiInterItem();
	InitBackgrandImages();
#ifndef MERGER_APP_TO_SHELL
	g_TBInvokeInterface.m_iInitialDeviceComm();
	SetForegroundWindow( m_hWnd );
#else
	g_TBInvokeInterface.m_iRegisterMpegCallback(APP_MPEG_CbkFunc);
//	g_TBInvokeInterface.m_iInitialDeviceComm();
//	g_TBInvokeInterface.m_iSWCEnterSWCCmd(0, 0);
#endif

	return TRUE;
}
void CMPEGShellWnd::OnMzCommand(WPARAM wParam, LPARAM lParam)
{

}
void CMPEGShellWnd::LoadiInterItem()
{
//jdhuang 20120818
#ifndef MERGER_APP_TO_SHELL
	m_MPEGShellMainBG = ImagingHelper::GetImageObject(MzGetInstanceHandle(), 1001);
#else
	m_MPEGShellMainBG = ImagingHelper::GetImageObject(g_SDHResModuleHandle, g_BkgndID);
#endif


}
BOOL CMPEGShellWnd::InitBackgrandImages()
{
	HDC hDC = GetDC(m_hWnd);
//jdhuang

	RECT rcClient;
	GetClientRect(m_hWnd, &rcClient);

	m_hBakDC = CreateCompatibleDC(hDC);

	m_hBitmapBakDC = CreateCompatibleBitmap(hDC, RECT_WIDTH(rcClient), RECT_HEIGHT(rcClient));
	SelectObject(m_hBakDC, m_hBitmapBakDC);

	//jdhuang 20090616
//	HDC hMemDc = CreateCompatibleDC(m_hBakDC);

	if(m_MPEGShellMainBG)
	{
		m_MPEGShellMainBG->Draw(m_hBakDC, &rcClient, false, false);
//		DeleteDC(hMemDc);
	}
	ReleaseDC(m_hWnd, hDC);

	return TRUE;
}
void CMPEGShellWnd::DeleteImages()
{
//	SAFE_DELETE_DC(m_hBakDC);
	if(m_hBakDC)
	{ 
		DeleteDC(m_hBakDC);
		m_hBakDC=NULL;
	}

//	SAFE_DELETE_OBJECT(m_hBitmapBakDC);
	if(m_hBitmapBakDC)
	{ 
		DeleteObject(m_hBitmapBakDC);
		m_hBitmapBakDC=NULL;
	}
}

LRESULT CMPEGShellWnd::MzDefWndProc(UINT message, WPARAM wParam, LPARAM lParam)
{
	switch(message)
	{
		case WM_CHANGE_BKGND_IMAGE:
		{
			g_BkgndID = LOWORD(wParam);
			ReloadBkgnd();			
			
			if(IsVisible())
				Invalidate();
			break;
		}
		case WM_CHANGE_LANGUAGE:
		{
//			m_UiDesktopIcon.UpdateLanguage();
			break;
		}
		case WM_ENDMODAL_WND:
		{
//			EndModal(ID_CANCEL);
			OnExit();
			break;
		}
	}
	return CMzWndEx::MzDefWndProc(message,wParam,lParam);
}
void CMPEGShellWnd::OnLButtonDown(UINT fwKeys, int xPos, int yPos)
{
	g_TBInvokeInterface.m_iMpegTouchPosCmd(xPos, yPos);
	g_dwScreenIdle = 0;
	CMzWndEx::OnLButtonDown(fwKeys, xPos, yPos);

}
void CMPEGShellWnd::OnLButtonUp(UINT fwKeys, int xPos, int yPos)
{
	g_TBInvokeInterface.m_iMpegTouchReleaseCmd(xPos, yPos);
	CMzWndEx::OnLButtonUp(fwKeys, xPos, yPos);

}


void CMPEGShellWnd::APP_MPEG_CbkFunc(PROTOCAL_INFO ProtocalInfo, BYTE bFlag)
{
#ifndef MERGER_APP_TO_SHELL
	CMPEGShellWnd *pdlg = (CMPEGShellWnd*)theApp.m_pMainWin;
#else
//	CMPEGShellWnd *pdlg = (CMPEGShellWnd*)g_pMPEGShellWnd;
	CMPEGShellWnd *pdlg = pgMPEGShellWnd;
#endif
	switch(bFlag)
	{
	case 0x0:
		{
			pdlg->SetCircle(TRUE);
		}
		break;
	case 0x1:
		{
			pdlg->SetCircle(FALSE);
		}
		break;
	}

}

BOOL CMPEGShellWnd::ReloadBkgnd()
{
	m_MPEGShellMainBG = ImagingHelper::GetImageObject(g_SDHResModuleHandle, g_BkgndID);
	InitBackgrandImages();
	return TRUE;
}
void CMPEGShellWnd::SetCircle(BOOL bCircle)
{
	RECT rcWaitDlg = {0, 0, 800, 480};
	if(bCircle && (!m_bIsCircleOpen))
	{
//		MzBeginWaitDlg(g_pMPEGShellWnd->m_hWnd, &rcWaitDlg, TRUE);
		MzBeginWaitDlg(pgMPEGShellWnd->m_hWnd, &rcWaitDlg, TRUE);
		m_bIsCircleOpen = TRUE;
//		SetTimer(g_pMPEGShellWnd->m_hWnd, 1000, 20000, NULL);
		SetTimer(pgMPEGShellWnd->m_hWnd, 1000, 20000, NULL);
	}
	else if((!bCircle) && m_bIsCircleOpen)
	{	
		MzEndWaitDlg();
		m_bIsCircleOpen = FALSE;
	}
}
void CMPEGShellWnd::OnTimer(UINT uID)
{
	if(uID == 1000)
	{	
//		KillTimer(g_pMPEGShellWnd->m_hWnd, 1000);
		KillTimer(pgMPEGShellWnd->m_hWnd, 1000);
		if(m_bIsCircleOpen)
		{
			SetCircle(FALSE);
//			printf("Kill Timer\r");
		}
	}
}
void CMPEGShellWnd::DestroyWindow()
{
	DeleteImages();
	PostQuitMessage(0);
	pgMPEGShellWnd = NULL;
	g_TBInvokeInterface.m_iRegisterMpegCallback(NULL);
	CMzWnd::DestroyWindow();
}
void CMPEGShellWnd::OnExit()
{

	DestroyWindow();

}